package cn.yiyuan.controller;

import cn.yiyuan.base.annotate.Authority;
import cn.yiyuan.service.IEmployeeService;
import cn.yiyuan.domain.Employee;
import cn.yiyuan.query.EmployeeQuery;
import cn.yiyuan.base.untli.AjaxResult;
import cn.yiyuan.base.untli.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {
    @Autowired
    public IEmployeeService employeeService;


    /**
     * 保存和修改公用的
     * @param employee  传递的实体
     * @return Ajaxresult转换结果
     */
    @Authority(name = "该方法是用于新增或修改")
    @PutMapping
    public AjaxResult insertOrUpdate(@RequestBody Employee employee){
        try {
            if( employee.getId()!=null)
                employeeService.update(employee);
            else
                employeeService.insert(employee);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
                employeeService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @Authority(name = "该方法是用于根据id查询")
    @GetMapping("/{id}")
    public AjaxResult getId(@PathVariable("id")Long id)
    {
        try {
            Employee employee = employeeService.getById(id);
            return AjaxResult.success(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @Authority(name = "该方法是用于获取全部")
    @GetMapping
    public AjaxResult getAll(){

        try {
            List< Employee> list = employeeService.getAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult DepList(@RequestBody EmployeeQuery query)
    {
        try {
            PageInfo<Employee> pageInfo = employeeService.querList(query);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！"+e.getMessage());
        }
    }


    /**
   * 批量删除
   */
    @PatchMapping
    public AjaxResult pachdelete(@RequestBody List<Long> ids)
    {
        try {
                employeeService.pachdelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量数据失败！"+e.getMessage());
        }
    }
}
